From 9cb61afea2739606eab580fc889e4d42e11eb3e8 Mon Sep 17 00:00:00 2001 From: robertl Date: Wed, 18 Sep 2002 14:40:54 +0000 Subject: [PATCH] From Alex Mottram: 1. add csv_util.c and csv_util.h 2. change mxf.c to use csv_util / minor other changes 3. change csv to use csv_util 4. add OziExplorer 1.1 file format. 5. remove the extra MXF section from the README Thanks, Alex --- Makefile | 12 ++++-- README | 7 +++- csv.c | 67 ++++++++++++++++++++++------- mxf.c | 125 ++++++++----------------------------------------------- testo | 4 ++ vecs.c | 6 +++ 6 files changed, 94 insertions(+), 127 deletions(-) diff --git a/Makefile b/Makefile index 6f324bfd2..a81e2e9d6 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,17 @@ CFLAGS=-g -Icoldsync FMTS=magproto.o gpx.o geo.o gpsman.o mapsend.o mapsource.o \ gpsutil.o tiger.o pcx.o csv.o cetus.o gpspilot.o magnav.o \ - psp.o mxf.o holux.o garmin.o + psp.o mxf.o holux.o garmin.o ozi.o JEEPS=jeeps/gpsapp.o jeeps/gpscom.o jeeps/gpsfmt.o jeeps/gpsinput.o \ jeeps/gpsmath.o jeeps/gpsmem.o \ jeeps/gpsproj.o jeeps/gpsprot.o jeeps/gpsread.o \ jeeps/gpsrqst.o jeeps/gpssend.o jeeps/gpsserial.o jeeps/gpsutil.o -OBJS=main.o queue.o route.o waypt.o util.o vecs.o mkshort.o \ - coldsync/util.o coldsync/pdb.o $(GARMIN) $(JEEPS) $(FMTS) +COLDSYNC=coldsync/util.o coldsync/pdb.o + +OBJS=main.o queue.o route.o waypt.o util.o vecs.o mkshort.o csv_util.o \ + $(COLDSYNC) $(GARMIN) $(JEEPS) $(FMTS) all: gpsbabel @@ -40,3 +42,7 @@ tiger.o: tiger.c defs.h queue.h magellan.h util.o: util.c defs.h queue.h vecs.o: vecs.c defs.h queue.h waypt.o: waypt.c defs.h queue.h +psp.o: psp.c defs.h queue.h +mxf.o: mxf.c csv_util.c defs.h queue.h csv_util.h +ozi.o: ozi.c csv_util.c defs.h queue.h csv_util.h +csv_util.o: csv_util.c csv_util.h defs.h diff --git a/README b/README index 7c502c5a1..62cf1375d 100644 --- a/README +++ b/README @@ -128,8 +128,11 @@ THE FORMATS complies with (at least) Maptech Terrain Navigator, Terrain Professional, Take a Hike, and ExpertGPS import/export MFX. - Maptech MXF - Maptech's MXF is a CSV on steroids. Contributed by Alex Mottram. + OZI + + OziExplorer Waypoint Format - Another CSV format file. + Tested against OziExplorer v 3.90.3a / Shareware. + Contributed by Alex Mottram COMMON USAGE diff --git a/csv.c b/csv.c index c2cd9c4f9..a7ba546a8 100644 --- a/csv.c +++ b/csv.c @@ -21,6 +21,7 @@ */ #include "defs.h" +#include "csv_util.h" #include static FILE *file_in; @@ -61,28 +62,62 @@ wr_deinit(void) static void data_read(void) { - char desc[80]; - char *odesc = desc; - double lat,lon; + char buff[1024]; + char *s; + int i; waypoint *wpt_tmp; + int linecount = 0; - while( fscanf(file_in, "%lf,%lf,%80[^\n]", - &lat, &lon, desc) > 0) { - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { + do { + linecount++; + memset(&buff, '\0', sizeof(buff)); + fgets(buff, sizeof(buff), file_in); + + if (strlen(buff)) { + + wpt_tmp = calloc(sizeof(*wpt_tmp), 1); + if (wpt_tmp == NULL) { fatal(MYNAME ": cannot allocate memory\n"); + } + + s = buff; + /* data delimited by commas, not enclosed */ + s = csv_lineparse(s, ",", "", linecount); + + i = 0; + + while (s) { + switch (i) { + case 0: + wpt_tmp->position.latitude.degrees = atof(s); + break; + case 1: + wpt_tmp->position.longitude.degrees = atof(s); + break; + case 2: + wpt_tmp->description = strdup(s); + if (! wpt_tmp->description) + fatal(MYNAME, ": cannot allocate memory\n"); + wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " "); + break; + default: + fprintf (stderr, "%s: Warning: unmapped data fields on line %d.\n", + MYNAME, linecount); + break; + } + i++; + + s = csv_lineparse(NULL, ",", "", linecount); } - while (*odesc == ' ' || *odesc == '\t') { - odesc++; - } - wpt_tmp->shortname = strdup(odesc); + wpt_tmp->creation_time = time(NULL); - - wpt_tmp->position.longitude.degrees = lon; - wpt_tmp->position.latitude.degrees = lat; - waypt_add(wpt_tmp); + + } else { + /* empty line */ } + + } while (!feof(file_in)); } static void @@ -92,6 +127,8 @@ gpsutil_disp(waypoint *wpt) lon = wpt->position.longitude.degrees; lat = wpt->position.latitude.degrees; + if (wpt->description) + wpt->description = csv_stringclean(wpt->description, ",\""); fprintf(file_out, "%08.5f, %08.5f, %s\n", lat, diff --git a/mxf.c b/mxf.c index aa13f1f19..47385f758 100644 --- a/mxf.c +++ b/mxf.c @@ -29,6 +29,7 @@ */ #include "defs.h" +#include "csv_util.h" #include #define MYNAME "MXF" @@ -36,108 +37,6 @@ static FILE *file_in; static FILE *file_out; -static char * -csvstringclean(char * string) -{ - static char * p1 = NULL; - char * p2 = NULL; - - if (! string) { - return (string); /* :) */ - } - - p2 = string; - - while ((*p2) && (p2++)) { } - p2--; - - while (isspace(*p2)) { - *p2 = '\0'; - p2--; - } - - p1 = string; - - while (isspace(*p1)) { - p1++; - } - - /* yank quotes in pairs only if they are bounding us */ - while ((*p1 == '"') && (*p2 == '"')) { - *p2 = '\0'; - p2--; - p1++; - } - - return (p1); -} - -/* string parser. sorta like strtok with quotes & pointers.. */ -/* designed to handle quoted and delimited data within quotes. */ -static char * -csvparse(char *stringstart, char *delimiter) -{ - char *sp; - static char *p = NULL; - static char *tmp = NULL; - size_t dlen; - int quotedepth = 0; - short int dfound; - - if (!p) { - p = stringstart; - - if (!p) { - return (NULL); - } - } - - if (tmp) { - free(tmp); - tmp = NULL; - } - - sp = p; - - dlen = strlen(delimiter); - dfound = 0; - - while ((*p) && (! dfound)) { - if (*p == '"') { - if (quotedepth) - quotedepth--; - else - quotedepth++; - } - - if ((!quotedepth) && (strncmp(p, delimiter, dlen) == 0)) { - dfound = 1; - - } else { - p++; - } - - } - - tmp = (char *) calloc((p - sp) + 1, sizeof(char)); - - if (! tmp) { - fatal(MYNAME ": cannot allocate memory\n"); - } - - strncpy(tmp, sp, (p - sp)); - - if (dfound) { - /* skip over the delimiter */ - p += dlen; - } else { - /* end of the line */ - p = NULL; - } - - return (tmp); -} - static void rd_init(const char *fname) { @@ -171,7 +70,7 @@ wr_deinit(void) static void data_read(void) { - char buff[256]; + char buff[1024]; char *s; waypoint *wpt_tmp; int i; @@ -190,8 +89,9 @@ data_read(void) fatal(MYNAME ": cannot allocate memory\n"); } + /* data delimited by commas, possibly enclosed in quotes. */ s = buff; - s = csvparse(s, ", "); + s = csv_lineparse(s, ",", "\"", linecount); i = 0; while (s) { @@ -203,10 +103,18 @@ data_read(void) wpt_tmp->position.longitude.degrees = atof(s); break; case 2: - wpt_tmp->description = strdup(csvstringclean(s)); + wpt_tmp->description = strdup(s); + if (! wpt_tmp->description) + fatal(MYNAME, ": cannot allocate memory\n"); + + wpt_tmp->description = csv_stringtrim(wpt_tmp->description, ""); break; case 3: - wpt_tmp->shortname = strdup(csvstringclean(s)); + wpt_tmp->shortname = strdup(s); + if (! wpt_tmp->shortname) + fatal(MYNAME, ": cannot allocate memory\n"); + + csv_stringtrim(wpt_tmp->shortname, ""); break; case 4: /* ignore. another name-type */ @@ -225,7 +133,7 @@ data_read(void) } i++; - s = csvparse(NULL, ", "); + s = csv_lineparse(NULL, ",", "\"", linecount); } if (i != 7) { @@ -249,6 +157,9 @@ mxf_disp(waypoint * wpt) int icon = 47; /* default to "dot" */ const char *color_hex = "ff0000"; + csv_stringclean(wpt->shortname, ",\""); + csv_stringclean(wpt->description, ",\""); + fprintf(file_out, "%08.5f, %08.5f, \"%s\", \"%s\", \"%s\", %s, %d\n", wpt->position.latitude.degrees, wpt->position.longitude.degrees, wpt->description, wpt->shortname, wpt->description, diff --git a/testo b/testo index 4699e1848..0b2c73d99 100755 --- a/testo +++ b/testo @@ -62,3 +62,7 @@ diff /tmp/ps.psp reference ${PNAME} -i geo -f geocaching.loc -o mxf -F /tmp/mxf.mxf diff /tmp/mxf.mxf reference +# OZI (OziExplorer 1.1) file format +${PNAME} -i geo -f geocaching.loc -o ozi -F /tmp/ozi.ozi +diff /tmp/ozi.ozi reference + diff --git a/vecs.c b/vecs.c index 6dceb8837..d2adab487 100644 --- a/vecs.c +++ b/vecs.c @@ -44,6 +44,7 @@ extern ff_vecs_t psp_vecs; extern ff_vecs_t garmin_vecs; extern ff_vecs_t mxf_vecs; extern ff_vecs_t holux_vecs; +extern ff_vecs_t ozi_vecs; static vecs_t vec_list[] = { @@ -127,6 +128,11 @@ vecs_t vec_list[] = { "holux", "Holux (gm-100) .wpo Format" }, + { + &ozi_vecs, + "ozi", + "OziExplorer Waypoint" + }, { NULL, -- 2.30.2